/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.edu.agh.soa.monitoring.endpoints.impl;

import java.util.List;
import java.util.Map;

import javax.jbi.management.DeploymentException;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.NormalizedMessage;

import org.apache.servicemix.common.Endpoint;
import org.apache.servicemix.common.endpoints.ProviderEndpoint;
import org.apache.servicemix.jbi.jaxp.StringSource;


import pl.edu.agh.soa.monitoring.endpoints.IAdministrationEndpoint;
import pl.edu.agh.soa.monitoring.endpoints.IReceiverEndpoint;
import pl.edu.agh.soa.monitoring.router.entry.DynamicRouterEntry;

/**
 * @org.apache.xbean.XBean element="administrationEndpoint"
 */
public class AdministrationEndpoint extends ProviderEndpoint implements IAdministrationEndpoint{
	
	IReceiverEndpoint receiverEndpoint;
	
    public void validate() throws DeploymentException {
    }
    
    @Override
	public void activate() {
		try {
			super.activate();
			for (Endpoint e : getServiceUnit().getEndpoints()) {
				if (e instanceof IReceiverEndpoint) {
					// we have found endpoint that have reference to esper
					// engine instance
					// we need to call registration operation on it
					receiverEndpoint = (IReceiverEndpoint) e;
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
    protected void processInOut(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception {
    	System.out.println("Got message on exchange: " + exchange);
    	if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
            // TODO tymczasowy interfejs do listowania mapy
    		StringBuffer sb = new StringBuffer();
    		sb.append("<routingMap>");
    		Map<String, List<DynamicRouterEntry>> routingMap = receiverEndpoint.getRoutingMap();
    		 for (String stmt : routingMap.keySet()){
    			 sb.append("Stmt: " +stmt +"\n");
    			 for(DynamicRouterEntry e: routingMap.get(stmt)){
    				 for (String s : e.getCallbackAddresses().keySet()){
    					 sb.append("\taddress: " + s + "\n");
	    					 for(String s2: e.getCallbackAddresses().get(s)){
	    						 sb.append("\t\tprotocol: " + s2 + "\n");
	    					 }
    					 }
    			 }
    		}	
    		sb.append("</routingMap>");
    		out.setContent(new StringSource(sb.toString()));
        }
    }

}
